$(function () {
    var filHeight = $('.mui-content').height();
    $('.filtrate_detail_box').css('height',filHeight+'px');

})

//筛选
// mui.init({
//swipeBack: false,
// });
//侧滑容器父节点
//var offCanvasWrapper = mui('#offCanvasWrapper');

//菜单容器
//var offCanvasSide = document.getElementById("offCanvasSide");

//移动效果是否为整体移动
//var moveTogether = false;
//侧滑容器的class列表，增加.mui-slide-in即可实现菜单移动、主界面不动的效果；
// var classList = offCanvasWrapper[0].classList;
// document.getElementById('offCanvasBtn').addEventListener('tap', function() {
//    offCanvasSide.classList.remove('mui-transitioning');
//    offCanvasSide.setAttribute('style', '');
//    classList.remove('mui-scalable');
//    classList.add('mui-slide-in');
//    offCanvasWrapper.offCanvas().refresh();
//    offCanvasWrapper.offCanvas('show');
// });
// document.getElementById('offCanvasHide').addEventListener('tap', function() {
//     offCanvasWrapper.offCanvas('close');
// });

//主界面和侧滑菜单界面均支持区域滚动；
// mui('#offCanvasSideScroll').scroll();
// mui('#offCanvasContentScroll').scroll();

//
var categoryId = GetQueryString('goodsCategory_id');
var keywords = GetQueryString('keywords');
var shopNum3;
var page = GetQueryString('page'),
    hotPage = GetQueryString('page'),
    page3 = 1;
page--;
hotPage--;
page3--;
var isDate = GetQueryString('isDate');
var isSales = GetQueryString('isSales');
var isPrice = null;

$(function(){
    if(isSales==0){
        $('#isSales').addClass('active');
        $('#isSales').siblings().removeClass('active');
        $('#synthesize_img').attr('src','../phoneImg/synthesize_sort.jpg');
    }
    if(keywords==""||keywords==undefined||keywords==null){
        if(categoryId==""||categoryId==undefined||categoryId==null){}else{isDate = 1;}
        dokeywords();
    }else{
        keywords = decodeURI(decodeURI(window.location.href)).split('=')[1].split('&')[0];
        $('#keyword').val(keywords);
        dokeywords();
    }
})


function dokeywords(){
    page3++;
    $.ajax({
        url:'/openSearchC/goodSearch.json',
        dataType:'json',
        data:{
            keywords:keywords,
            page:page3,
            unCategory_id:categoryId,
            isDate:isDate,
            isSales:isSales,
            isPrice:isPrice,
            companyCode:getItem('companyCode'),
        },
        type:'post',
        beforeSend:function(){
            if(page3==1){
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.list_box2'));
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.list_box'));
            }
        },
        success:function(data){
            var data2 = data.datas;
            if(page3==1){
                $('.list_box').empty();
                $('.list_box2').empty();
                if(data2.length==0){
                    mui('#offCanvasContentScroll').pullRefresh().disablePullupToRefresh();
                    $('<div class="kong"><img src="../phoneImg/kong.png" alt="暂无数据"></div>').appendTo($('.list_box'));
                    $('<div class="kong"><img src="../phoneImg/kong.png" alt="暂无数据"></div>').appendTo($('.list_box2'));
                }
            }
            for(var i=0;i<data2.length;i++){
                $('<div class="item_box mui-clearfix" goodsId="'+data2[i].goods_id+'">'+
                    '<div class="img"><img src="'+data2[i].main_img.split(',')[0]+'" alt=""></div>'+
                    '<div class="goods">'+
                    '<div class="title">'+data2[i].name+'</div>'+
                    '<div class="tips mui-clearfix"><span>正品保证</span><span>包邮</span><span>线上线下</span></div>'+
                    '<div class="price"><b>￥</b>'+data2[i].member_price+'<span><img src="../phoneImg/stages_icon2.png" alt=""></span></div>'+
                    '<div class="comment"><span>'+data2[i].commentNum+'人评价</span><span>'+data2[i].footMarkNum+'次浏览</span></div>'+
                    '</div></div>').appendTo($('.list_box'))
            }
            for(var i=0;i<data2.length;i++){
                $('<div class="item2_box" goodsId="'+data2[i].goods_id+'">'+
                    '<div class="img"><img src="'+data2[i].main_img.split(',')[0]+'" alt=""></div>'+
                    '<div class="goods">'+
                    '<div class="title">'+data2[i].name+'</div>'+
                    '<div class="tips mui-clearfix"><span>正品保证</span><span>包邮</span><span>线上线下</span></div>'+
                    '<div class="stages mui-clearfix"><span><img src="../phoneImg/stages_icon.png" alt=""></span></div>'+
                    '<div class="price"><b>￥</b>'+data2[i].member_price+'</div>'+
                    '<div class="comment"><span>'+data2[i].commentNum+'人评价</span><span>'+data2[i].footMarkNum+'次浏览</span></div>'+
                    '</div></div>').appendTo($('.list_box2'))
            }
            shopNum3 = data2.length;
        }
    })
    return shopNum3;
}

//上拉加载
mui.init({
    pullRefresh : {
        container:"#offCanvasContentScroll",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        down: {
            contentdown : "下拉可以刷新",//可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
            contentover : "释放立即刷新",//可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
            contentrefresh : "正在刷新...",//可选，正在刷新状态时，下拉刷新控件上显示的标题内容
            callback: function () {
                window.location.reload();
                mui('#offCanvasContentScroll').pullRefresh().endPulldownToRefresh();
            }
        },
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult;
                    numResult = dokeywords();
                    if (numResult < 20) {
                        mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});


//跳转商品详情页
mui('body').on('tap','.item_box,.item2_box',function(){
    var goodsId = $(this).attr('goodsId');

    window.location.href="goods.html?goodsId="+goodsId;
})

//更改列表结构
document.getElementById('search-right').addEventListener('tap',function () {
    document.documentElement.scrollTop = document.body.scrollTop =0;
    if($(this).attr('list')==1){
        $('.list_box').hide();
        $('.list_box2').show();
        $(this).attr('list','2');
        $(this).css({'background':'url("../phoneImg/goods_list1.png") no-repeat 0 0','background-size':'cover'})
    }else{
        $('.list_box2').hide();
        $('.list_box').show();
        $(this).attr('list','1');
        $(this).css({'background':'url("../phoneImg/goods_list2.png") no-repeat 0 0','background-size':'cover'})
    }
})

//销量
document.getElementById('isSales').addEventListener('tap',function () {
    $(this).addClass('active');
    $(this).siblings().removeClass('active');
    $('#synthesize_img').attr('src','../phoneImg/synthesize_sort.jpg');
    isDate = null;
    isSales = 1;
    isPrice = null;
    page3=0;
    dokeywords();
})

//综合
document.getElementById('isDate').addEventListener('tap',function () {
    $(this).addClass('active');
    $(this).siblings().removeClass('active');
    $('#synthesize_img').attr('src','../phoneImg/synthesize_sort_act.jpg');
    isDate = 1;
    isSales = null;
    isPrice = null;
    page3=0;
    dokeywords();
})

//价格
document.getElementById('isPrice').addEventListener('tap',function () {
    if($(this).attr('isPrice')==""||$(this).attr('isPrice')==0){
        $('.price').attr('src','../phoneImg/price_sort1.png');
        isPrice = 1;
        $(this).attr('isPrice','1');
    }else{
        $('.price').attr('src','../phoneImg/price_sort2.png');
        isPrice = 0;
        $(this).attr('isPrice','0');
    }
    $(this).addClass('active');
    $(this).siblings().removeClass('active');
    $('#synthesize_img').attr('src','../phoneImg/synthesize_sort.jpg');
    isDate = null;
    isSales = null;
    page3=0;
    dokeywords();
})

//足迹
document.getElementById('spoor').addEventListener('tap',function () {
    window.location.href="spoor.html";
})

//搜索
document.getElementById('keyword').addEventListener('tap',function () {
    window.location.href="search.html";
})

//mui下拉去掉一个滚动条
// mui('.mui-scroll-wrapper').scroll({
//     indicators: true, //是否显示滚动条
// });
document.getElementById('offCanvasContentScroll').addEventListener("swiperight",function(){
    history.back();
});